import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    userinfo:sessionStorage.getItem('userinfo')?JSON.parse(sessionStorage.getItem('userinfo')): null
  },
  getters:{
    getUser(state){
      return state.userinfo
    }
  },
  mutations: {
    CHANGE_USER(state,payload){
      state.userinfo = payload
      if(payload){
        // 把数据顺便存储到会话存储中
        sessionStorage.setItem('userinfo',JSON.stringify(payload))
      }else{
        //移除存储信息
        sessionStorage.removeItem('userinfo')
      }
    }
  },
  actions: {
    //封装一个修改state的行动
    changeUser({commit},payload){
      console.log('行动');
      commit('CHANGE_USER',payload)
    }
  },
  modules: {
  }
})
